package TankCampaignGame.com.sjw.Game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import java.util.Scanner;
import java.util.Vector;

/*
游戏窗口绘制
 */
public class MyPanel extends JPanel implements KeyListener,Runnable{

    Hero h = null;
    Vector<Enemy> enemies= new Vector<>();
    int enemySize = 6;
    public MyPanel(){
        h = new Hero(450,850);
        //创建敌方坦克
        for(int i = 0; i < enemySize; i++){
            Enemy enemy = new Enemy(100*(i+1),0);
            enemy.setDirect(2);
            //创建发射子弹线程
            BulletThread bulletThread = new BulletThread(new Bullet(enemy.getX() + 20,enemy.getY() + 60,enemy.getDirect()));
            bulletThread.start();
            enemy.vector.add(bulletThread);
            enemies.add(enemy);
        }

    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawRect(0,0,1000,1000);
        g.setColor(Color.black);
        g.fillRect(0,0,1000,1000);
        drawTank(g,h.getX(),h.getY(),h.getDirect(),0);

        //画出子弹
        for(int i = 0; i < h.bulletThreads.size();i++){
            BulletThread bulletThread = h.bulletThreads.get(i);
            if(bulletThread != null && h.bullet != null && h.bullet.isLive() != false){
                g.fill3DRect(h.bullet.getX(),h.bullet.getY(),5,5,false);
            }else {
                h.bulletThreads.remove(bulletThread);
            }
        }
        //画敌方坦克
        for(int i = 0; i < enemies.size(); i++){
            Enemy enemy = enemies.get(i);
            drawTank(g,enemy.getX(),enemy.getY(),enemy.getDirect(),1);
            //画出子弹
            for(BulletThread bulletThread : enemy.vector){
                if(bulletThread.bullet.isLive()){
                    g.fill3DRect(bulletThread.bullet.getX(),bulletThread.bullet.getY(),5,5,false);
                }else{
                    enemy.vector.remove(bulletThread);
                }
            }
        }
    }

    /**
     *
     * @param g 画笔
     * @param direct 坦克的方向
     * @param type 坦克的类型
     */
    public void drawTank(Graphics g,int x,int y,int direct,int type){
        switch (type){
            case 0://友方
                g.setColor(Color.cyan);
                break;
            case 1://敌方
                g.setColor(Color.yellow);
                break;
        }
        switch (direct){
            case 0: //坦克方向向上
                g.fill3DRect(x,y,10,60,false); //车轮
                g.fill3DRect(x+30,y,10,60,false); //车轮
                g.fill3DRect(x+10,y+10,20,40,false); //身体
                g.drawLine(x+20,y+30,x+20,y); //炮筒
                g.fillOval(x+10,y+20,20,20); //炮台
                break;
            case 1: //坦克方向向右
                g.fill3DRect(x,y,60,10,false); //车轮
                g.fill3DRect(x,y+30,60,10,false); //车轮
                g.fill3DRect(x+10,y+10,40,20,false); //身体
                g.drawLine(x+30,y+20,x+60,y+20); //炮筒
                g.fillOval(x+20,y+10,20,20); //炮台
                break;
            case 2: //坦克方向向下
                g.fill3DRect(x,y,10,60,false); //车轮
                g.fill3DRect(x+30,y,10,60,false); //车轮
                g.fill3DRect(x+10,y+10,20,40,false); //身体
                g.drawLine(x+20,y+30,x+20,y+60); //炮筒
                g.fillOval(x+10,y+20,20,20); //炮台
                break;
            case 3: //坦克方向向左
                g.fill3DRect(x,y,60,10,false); //车轮
                g.fill3DRect(x,y+30,60,10,false); //车轮
                g.fill3DRect(x+10,y+10,40,20,false); //身体
                g.drawLine(x+30,y+20,x,y+20); //炮筒
                g.fillOval(x+20,y+10,20,20); //炮台
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){
            h.setDirect(0); //向上
            h.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_S){
            h.setDirect(2); //向下
            h.moveDown();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            h.setDirect(1); //向右
            h.moveRight();
        }else if(e.getKeyCode() == KeyEvent.VK_A){
            h.setDirect(3); //向左
            h.moveLeft();
        }
        if(e.getKeyCode() == KeyEvent.VK_J){
            h.shooting();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}
